<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.stone.para.IConstant"%>
<%@ page import="com.stone.sys.pojo.TOrgan"%>
<%
	String contextPath = request.getContextPath();

	String bdz = (String) request.getParameter("bdz");
	String bdzname = (String) request.getParameter("bdzname");	
	String ouid = (String) request.getParameter("ouid");
	
	String loginOrgan = request.getParameter("loginOrgan") != null ? request.getParameter("loginOrgan") : ((TOrgan)session.getAttribute(IConstant.SESSION_TORGAN)).getId().toString();
	
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=IConstant.PROGRAEM_TITLE%></title>
        <link rel="stylesheet" href="<%=contextPath%>/ext3/resources/css/ext-all.css" type="text/css"   />
        <script src="<%=contextPath%>/ext3/adapter/ext/ext-base.js"></script>
        <script src="<%=contextPath%>/ext3/ext-all.js"></script>
        <script language="javascript">
            Ext.BLANK_IMAGE_URL = '<%=contextPath%>/ext3/resources/images/default/s.gif';
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'qtip';
            var cpath = '<%=contextPath%>';
		    var checking = false;
		    var tree;
		    var oOption;

		    String.prototype.endWith=function(str){
		    	if(str==null||str==""||this.length==0||str.length>this.length)
		    	return false;
		    	if(this.substring(this.length-str.length)==str)
		    	return true;
		    	else
		    	return false;
		    	return true;
		    	}
		   
			Ext.onReady(function(){
			
			var tb = new Ext.Toolbar({
		    renderTo: document.all.tbar,
		    items: [
 				'->',{
			        text: '确定',
			        icon:"<%=contextPath%>/images/pass.gif",
			        handler:setUser
			    }//,'-',
			   // {
			      //  text: '清空',
			      //  icon:"<%=contextPath%>/images/clear.png",
			       // handler:function() {
						// 变电站ID
						//window.opener.document.frmM.elements["<%=bdz%>"].value = "";

						// 变电站名
						//window.opener.document.frmM.elements["<%=bdzname%>"].value = "";

						// 变电站OUID
						//window.opener.document.frmM.elements["<%=ouid%>"].value = "";
			 			
			      //  }
			   // }
		    ]
			});
		
			var storeSQ = new Ext.data.Store({
        		proxy:new Ext.data.HttpProxy({url:'userList!getListExtTree.do'}),
        		reader:new Ext.data.JsonReader({},['id','text','qtip','parentId']),
        		autoLoad:true,
        		listeners:{"load":function(store,records){
        		
        			if (records != null && records != "" && records.length > 0) {
        							
        				var baseLength = (records[0].data.qtip + "").length;

						// 已选择部门
        				var arrayoldid = window.opener.document.frmM.elements["<%=bdz%>"].value.split(",");
        				
        								
						for (var i=0; i<records.length; i++){
						
	       					var n = new Ext.tree.TreeNode({
                								id:records[i].data.id,
                								text:records[i].data.text,
                								qtip:records[i].data.qtip,
                								leaf:true,
                								checked:false
							});
											
							// 第一层菜单时
							if (i == 0) {

								tree.root.appendChild(n);
								
								// 下级菜单
							} else {
							
								try {
    								tree.getNodeById(records[i].data.parentId).leaf = false;
    								tree.getNodeById(records[i].data.parentId).appendChild(n);
    											
    							} catch (e) {
    							}
								
							}
   						}

						checking = true;
						
						// 选中已有的人员
						for (var j=0; j<arrayoldid.length; j++) {
							if (arrayoldid[j] != "") {
								tree.getNodeById(arrayoldid[j]).attributes.checked = true;
								checkParent(tree.getNodeById(arrayoldid[j]));
							}
						}
		
						checking = false;
						//tree.expandAll();
						
						if (arrayoldid.length ==0 || (arrayoldid.length ==1 && arrayoldid[0]=="")) {
						
					        tree.root.expandChildNodes();
							// 变电工区展开
							tree.root.firstChild.expandChildNodes();//    tree.getNodeById("A1").expandChildNodes();
							for(var i =0;i<tree.root.firstChild.childNodes.length;i++) {
								if(tree.root.firstChild.childNodes[i].id  != tree.getNodeById("<%=loginOrgan%>").id) {
									tree.root.firstChild.childNodes[i].collapse();
								}
							}
					       // tree.getNodeById("<%=loginOrgan%>").expandChildNodes();  
					          
						} else {
							// root 展开
							tree.root.expandChildNodes();
							// 变电工区展开
							tree.root.firstChild.expandChildNodes();//    tree.getNodeById("A1").expandChildNodes();
							for(var i =0;i<tree.root.firstChild.childNodes.length;i++) {
								if(tree.root.firstChild.childNodes[i].id  != tree.getNodeById(arrayoldid[0]).parentNode.id) {
									tree.root.firstChild.childNodes[i].collapse(true);
								}
							}
							tree.getNodeById(arrayoldid[0]).parentNode.expandChildNodes();
						}
				
					}   // if (records != null && records != "" && records.length > 0) {
        	}} // 
                			 
		});

            
		/* 生成Ttee,放到tree Div里面 */
 		tree = new Ext.tree.TreePanel({
                        id:"mytree",   
                        renderTo : "tree",
                        rootVisible:false,
                        root : new Ext.tree.TreeNode({   
                    			id : "root", 
                    			draggable:false, 
                    			singleClickExpand:true, 
                    			text : "<%=IConstant.PROGRAEM_TITLE%>"
	 						}),  
                        store : storeSQ,   
                        width : 300,   
                        height : 480,
				        useArrows:true,
				        autoScroll:true,
				        animate:true,
				        containerScroll:true,
				        checked:false,
				        listeners:{"click":function(node){ 
												
														// 参照
														try {
															window.opener.document.frmM.elements["<%=bdz%>"].value = node.id;
															window.opener.document.frmM.elements["<%=bdzname%>"].value = node.text;
															window.opener.document.frmM.elements["<%=ouid%>"].value = "," + node.attributes.qtip + ",";
														} catch (e) {
														
														}
														window.close();	
													
                    	}}
				});
				
		// checkbox触发事件
		tree.on('checkchange', function(node,checked) { 
	
			if(checking){ 
				return false;
			}
				
			checking = true;
			// 选中的话
			if(node.ui.isChecked()){
				checkChildren(node,true);
				checkParent(node);
			
			// 没选中
			} else {
				checkChildren(node,false);
				uncheckParent(node);
			}
		
			checking = false;
		});
				
				
		// 子结点处理
		function checkChildren(node, checked)
		{
			//for(var i =0;i<node.childNodes.length;i++) {
			//	node.childNodes[i].ui.toggleCheck(checked);
			//	checkChildren(node.childNodes[i], checked);
			//}
	       node.attributes.checked = checked;
	       node.ui.toggleCheck(checked);
	       node.eachChild(function(child){
	         checkChildren(child,checked);
	       }); 
		}
	
		// 选中所有父结点
		function checkParent(node) {
	
			if(node.parentNode == null){
				return false;
			}
			node.parentNode.attributes.checked = true;
			node.parentNode.ui.toggleCheck(true);
			checkParent(node.parentNode);
		}
	
		// 取消所有父结点
		function uncheckParent(node) {
			if(node.parentNode == null) {
				return false;
			}
	
			var blCheck = true;
			for(var i =0;i<node.parentNode.childNodes.length;i++) {
				if(node.parentNode.childNodes[i].ui.isChecked()) {
					blCheck = false;
				}
			}
			if(blCheck) {
				node.parentNode.ui.toggleCheck(false);
				uncheckParent(node.parentNode);
			}
		}

 	});
 		
 		
		// 过滤选择选中的复选框
		function getCheckMenuID(node) {

			if (node.ui.isChecked()) {

				if ((node.attributes.qtip + "" ).length == 9) {

					// 变电站ID
					window.opener.document.frmM.elements["<%=bdz%>"].value = 
						window.opener.document.frmM.elements["<%=bdz%>"].value + node.id + ",";
	
					// 变电站名
					window.opener.document.frmM.elements["<%=bdzname%>"].value = 
						window.opener.document.frmM.elements["<%=bdzname%>"].value + node.text + ",";
	
					// 变电站OUID
					window.opener.document.frmM.elements["<%=ouid%>"].value = 
						window.opener.document.frmM.elements["<%=ouid%>"].value + node.attributes.qtip + ",";

				}

			}
		
		    for (var i=0;i<node.childNodes.length;i++) {
    			getCheckMenuID(node.childNodes[i]);
    		}
		}

 		function setUser() {

			// 变电站ID
			window.opener.document.frmM.elements["<%=bdz%>"].value = "";

			// 变电站名
			window.opener.document.frmM.elements["<%=bdzname%>"].value = "";

			// 变电站OUID
			window.opener.document.frmM.elements["<%=ouid%>"].value = ",";
 			
 			for (var i=0;i<tree.root.childNodes.length;i++) {
    			getCheckMenuID(tree.root.childNodes[i]);
    		}

 			var bdz = window.opener.document.frmM.elements["<%=bdz%>"].value;
 			var bdzname = window.opener.document.frmM.elements["<%=bdzname%>"].value
 			
 			if (bdz.endWith(",")) {
 				window.opener.document.frmM.elements["<%=bdz%>"].value = 
 					bdz.substring(0,bdz.length-1);
 			}
 			
 			if (bdzname.endWith(",")) {
 				window.opener.document.frmM.elements["<%=bdzname%>"].value = 
 					bdzname.substring(0,bdzname.length-1);
 			}
 			
 			window.close();
 		}
 		

		</script>
	</head>
	<body>
		<div id="tbar" top="0px" style="text-align:right"></div>
		<div id="tree" style="position:absolute;left:0px;top:25px;width:100%;height:100%;overflow:hidden;border:none"></div>
	</body>
</html>
